﻿using System.Collections;
using System.IO.Ports;
using TYBase;

namespace TYRTUBase
{
    public class TYSysRTU : TYRTUBase
    {
        public override string ParaKey()
        {
            return $"{Port}";
        }


        public override bool IsConnected
        {
            get { return serialPort != null && serialPort.IsOpen; }
        }

        SerialPort serialPort = null;

        public TYSysRTU(int _port, ComPara para) : base(_port, para)
        {
        }

        //private PComm pcomm;

        public override bool ClearInBuff()
        {
            if (serialPort == null)
                return false;
            serialPort.DiscardInBuffer();
            return true;
        }

        public override bool Connect()
        {
            Log.I($"ir Connect:{GetPara()}");
            try
            {
                if (serialPort == null)
                    serialPort = new SerialPort()
                    {
                        BaudRate = BaudRate,
                        PortName = $"COM{Port}",
                        Parity = Parity,
                        DataBits = DataBits,
                        StopBits = StopBits,
                        NewLine = NewLine.AsHexString(),
                    };
                serialPort.Open();
                return serialPort.IsOpen;
            }
            catch (System.Exception ex)
            {
                Log.I($"sys rtu {GetPara()} error : {ex.ExceptionInfo()}");
                return false;
            }
        }

        public override void Close()
        {
            if (serialPort == null)
            {
                return;
            }

            serialPort.Close();
        }


        public override int Write(byte[] byteData, int offset = 0, int length = -1)
        {
            if (serialPort == null || byteData == null || serialPort.IsOpen == false)
            {
                return -1;
            }
            if (length == -1)
            {
                Log.I($"com:{Port} send :{byteData.AsHexString()} ");
                serialPort.Write(byteData, offset, byteData.Length);
                FireOnComDataSend(this, byteData);
                return byteData.Length;
            }
            else
            {
                Log.I($"com:{Port} send :{byteData.AsHexString()} ");
                serialPort.Write(byteData, offset, length);
                FireOnComDataSend(this, byteData);
                return length;
            }
        }

        public override int Read(out byte[] byteData, int length = -1)
        {
            if (serialPort == null || serialPort.IsOpen == false)
            {
                byteData = null;
                return -1;
            }

            int readLength = serialPort.BytesToRead;
            Log.I($"com:{Port} read :{readLength} ");
            if (readLength > 0)
            {
                byteData = new byte[readLength];
                if (length == -1)
                {
                    int len = serialPort.Read(byteData, 0, readLength);
                    {
                        Log.I($"com:{Port} read :{byteData.AsHexString()} ");
                        FireOnComDataRead(this, byteData);
                        return readLength;
                    }
                }
                else
                {
                    byteData = new byte[length];
                    int len = serialPort.Read(byteData, 0, length);
                    {
                        FireOnComDataRead(this, byteData);
                        return length;
                    }
                }
            }
            byteData = null;
            return -1;
        }

        public override string GetInfo()
        {
            return "";
        }

        //后续处理得到当前参数的消息,例如端口等消息
        public override string GetPara()
        { 
            return $"PortName:{Port} BaudRate:{BaudRate} Parity:{Parity} DataBits:{DataBits} StopBits:{StopBits}";
        }

    }
}